package com.letter.otools.day.util;

import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES;

import android.app.UiModeManager;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDelegate;

public class AppUtils {

    public static int getAppNightMode() {
        return SPUtils.getInstance().getInt("appNightMode", MODE_NIGHT_NO);
    }

    public static void setNightMode(Context context, int nightMode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            UiModeManager uiManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
//                            uiManager.enableCarMode(0);
            uiManager.setNightMode(nightMode);
        } else {
            AppCompatDelegate.setDefaultNightMode(nightMode);
        }
        SPUtils.getInstance().put("appNightMode", nightMode);
    }

    public static int getNightMode(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            UiModeManager uiManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
            return uiManager.getNightMode();
        } else {
            return AppCompatDelegate.getDefaultNightMode();
        }
    }

    public static void copy2ClipboardManager(Context context, String text) {
        ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        clipboardManager.setText(text); // 复制
        Toast.makeText(context, "Content copied", Toast.LENGTH_SHORT).show();
    }

    @NonNull
    public static String getAppVersionName(Context context, final String packageName) {
        if (TextUtils.isEmpty(packageName)) return "";
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(packageName, 0);
            return pi == null ? "" : pi.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return "";
        }
    }
}
